package com.admission.web.admin;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;
import org.xml.sax.SAXException;

import com.admission.model.EnrollDetail;
import com.admission.service.EnrollDetailManager;
import com.admission.util.DeleteFile;
import com.admission.util.FileFilter;
import com.admission.util.NewFileName;
import com.admission.util.PageUtils;
import com.admission.util.StaticEnroll;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class EnrollDetailAdminAction extends CrudActionSupport<EnrollDetail> {

	private static final long serialVersionUID = 8588210870237681938L;
	private String id;
	private EnrollDetail entity;
	private EnrollDetailManager enrollDetailManager;
	private Page<EnrollDetail> page = new Page<EnrollDetail>(
			PageUtils.DEFAULT_PAGE_SIZE);
	private File[] enrollFile;
	private String[] enrollFileFileName;
	private String ids;
	private StaticEnroll staticEnroll;
	private String year;
	private String province;
	private String type;

	public EnrollDetail getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		enrollDetailManager.getTitleList(page);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		String uploadPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		String[] subPath;
		try {
			if (enrollFile != null) {
				subPath = new String[enrollFile.length];
				for (int i = 0; i < enrollFile.length; i++) {
					subPath[i] = NewFileName.genNewFileName(
							enrollFileFileName[i], i);
					FileUtils.copyFile(enrollFile[i], new File(uploadPath,
							subPath[i]));

					enrollDetailManager.save(uploadPath + File.separator
							+ subPath[i]);

				}

			}

			staticEnroll.createHtmlForEnrollDetail();
		} catch (Exception e) {
			this.addActionMessage("请按照招生计划模板表进行编辑，详细错误信息可在日志管理处查看，如果没有权限可联系超级管理员");
			return INPUT;
		}
		return list();
	}

	public String recovery() throws IOException, IllegalArgumentException,
			SAXException, IllegalAccessException {
		// 删除静态网页
		String regx = "enroll-detail-.*";
		File f = new File(ServletActionContext.getServletContext().getRealPath(
				"/statichtml"));
		File[] s = f.listFiles(new FileFilter(regx));
		for (File file : s) {
			DeleteFile.deleteByFile(file);
		}
		staticEnroll.createHtmlForEnrollDetail();
		Struts2Utils.renderText("刷新成功");
		return null;
	}

	@Override
	public String delete() throws Exception {
		try {
			enrollDetailManager.delete(ServletActionContext.getServletContext()
					.getRealPath("/statichtml"), year, province, type);
			// this.addActionMessage("操作成功");
		} catch (Exception e) {
			this.addActionMessage("操作失败，请重新操作");
		}
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new EnrollDetail();
		} else {
			entity = enrollDetailManager.getById(id);
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public File[] getEnrollFile() {
		return enrollFile;
	}

	public void setEnrollFile(File[] enrollFile) {
		this.enrollFile = enrollFile;
	}

	public String[] getEnrollFileFileName() {
		return enrollFileFileName;
	}

	public void setEnrollFileFileName(String[] enrollFileFileName) {
		this.enrollFileFileName = enrollFileFileName;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Autowired
	public void setStaticEnroll(StaticEnroll staticEnroll) {
		this.staticEnroll = staticEnroll;
	}

	public EnrollDetail getEntity() {
		return entity;
	}

	public void setEntity(EnrollDetail entity) {
		this.entity = entity;
	}

	public Page<EnrollDetail> getPage() {
		return page;
	}

	public void setPage(Page<EnrollDetail> page) {
		this.page = page;
	}

	@Autowired
	public void setEnrollDetailManager(EnrollDetailManager enrollDetailManager) {
		this.enrollDetailManager = enrollDetailManager;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getYearOption() {
		List<String> l = enrollDetailManager.getYears();
		String r = "";
		for (String s : l) {
			r += "<option value='" + s + "' >" + s + "</option>";
		}
		return r;
	}

}
